package grouplife::Controller::TuanZhang;
use Moose;
use namespace::autoclean;
use Catalyst::Request::Upload;
use Catalyst::Log;
use grouplife::Model::Users::UsersSearchModel;
use grouplife::Model::Users::UserRegisterModel;
use grouplife::Model::Comments::CommentsForUsersModel;
use grouplife::Model::UserUser::UserUserModel;
use grouplife::Model::UserStore::UserStoreModel;
use grouplife::Model::Tuan::TuansSearchModel;
use grouplife::Model::CommentedItem::CommentedItemModel;
use grouplife::Model::University::UniversityModel;
use Util::Email::SendEmail;
use Digest::MD5;

BEGIN {extends 'Catalyst::Controller'; }
__PACKAGE__->config( 'Plugin::ConfigLoader' => { file => 'grouplife.yml' } );

=head1 NAME

grouplife::Controller::Users - Catalyst Controller

=head1 DESCRIPTION

Catalyst Controller.

=head1 METHODS

=cut


=head2 index

=cut

sub base :Chained('/') :PathPart('tuanzhang') :CaptureArgs(0) {
        my ($self, $c) = @_;
}

sub object :Chained('base') :PathPart('id') :CaptureArgs(1) {
        my ($self, $c, $id) = @_;
        $c->stash(object => $c->model('TuanZhang::TuanZhangModel')->find($id));
        $c->stash(id => $id);
}

sub pizhun : Local {
	my ($self, $c) = @_;
	if (!$c->user_exists) {
                $c->response->redirect($c->uri_for('/login'));
                $c->flash->{redirect_after_login} = '' . $c->req->uri;
                return 0;
        }
	my $tuanzhang_id = $c->request->params->{tuanzhang_id};
	my $user_id = $c->session->{login_user}->id;
        my $request = $c->model('TuanZhang::TuanZhangModel')->find($tuanzhang_id);
	my $current_id = $c->session->{login_user}->id;
	my $is_admin = $c->model('UserTuan::UserTuanModel')->is_admin($c->session->{login_user},$c->session->{login_user});
	if ($is_admin ne '1') {
		my $message = "只有管理员可以审批营长认证申请";
		$c->stash(login_user => $c->session->{login_user});
                $c->stash(css   => 'tuanzhang.css');
                $c->stash(message => $message);
                $c->stash(request => $request);
                $c->stash(template => 'tuanzhang/tuanzhang_info_failed.tt2');
		return 1;
	}
	if ($request->status ne 'R') {
		my $message = "当前营长认证请求已经被批准或拒绝过，无法再次批准";
		$c->stash(login_user => $c->session->{login_user});
                $c->stash(css   => 'tuanzhang.css');
                $c->stash(message => $message);
                $c->stash(request => $request);
                $c->stash(template => 'tuanzhang/tuanzhang_info_failed.tt2');
		return 1;
	}
        $request = $c->model('TuanZhang::TuanZhangModel')->update_status($tuanzhang_id,'Y',$user_id);
	my $sender = Util::Email::SendEmail->new();
        my $tuanzhang_url = $c->config->{tuanzhang_info_uri} . "/" . $request->id. "/tuanzhang_info";
        my (%params, %options);
        $options{INCLUDE_PATH} = $c->config->{email_template_path};
        $params{tuanzhang_request} = $request;
        $params{approve_time} = `date`;
        $params{conf_url} = $tuanzhang_url;
        $params{username} = $request->users->username;
        my $title = "您已通过拼单族营长身份认证";
        my $template = "email/approveTuanZhang.html.tt";
	my $tuanzhanguser = $c->model('Users::UsersSearchModel')->find($request->user_id);
        $sender->send($tuanzhanguser->email_address, $title, $template, \%params, \%options);
        $c->stash(
		login_user => $c->session->{login_user},
                css => 'tuanzhang.css',
                title => '营长认证申请',
                tuanzhang => $request,
                javascript => '/tuan/tuan_info.js',
                template => 'tuanzhang/tuanzhang_request_mainpage.tt2');
}

sub jujue : Local {
	my ($self, $c) = @_;
	if (!$c->user_exists) {
                $c->response->redirect($c->uri_for('/login'));
                $c->flash->{redirect_after_login} = '' . $c->req->uri;
                return 0;
        }
	my $tuanzhang_id = $c->request->params->{tuanzhang_id};
	my $shenpi_comment = $c->request->params->{reject_comment_hidden};
	my $user_id = $c->session->{login_user}->id;
        my $request = $c->model('TuanZhang::TuanZhangModel')->find($tuanzhang_id);
	my $current_id = $c->session->{login_user}->id;
	my $is_admin = $c->model('UserTuan::UserTuanModel')->is_admin($c->session->{login_user},$c->session->{login_user});
	if ($is_admin ne '1') {
		my $message = "只有管理员可以拒绝营长认证申请";
		$c->stash(login_user => $c->session->{login_user});
                $c->stash(css   => 'tuanzhang.css');
                $c->stash(message => $message);
                $c->stash(request => $request);
                $c->stash(template => 'tuanzhang/tuanzhang_info_failed.tt2');
		return 1;
	}
	if ($request->status ne 'R') {
		my $message = "当前营长认证请求已经被批准或拒绝过，无法再次拒绝";
		$c->stash(login_user => $c->session->{login_user});
                $c->stash(css   => 'tuanzhang.css');
                $c->stash(message => $message);
                $c->stash(request => $request);
                $c->stash(template => 'tuanzhang/tuanzhang_info_failed.tt2');
		return 1;
	}
        $request = $c->model('TuanZhang::TuanZhangModel')->reject_renzheng($tuanzhang_id,'N',$user_id,$shenpi_comment);
	my $sender = Util::Email::SendEmail->new();
        my $tuanzhang_url = $c->config->{tuanzhang_info_uri} . "/" . $request->id. "/tuanzhang_info";
        my (%params, %options);
        $options{INCLUDE_PATH} = $c->config->{email_template_path};
        $params{tuanzhang_request} = $request;
        $params{approve_time} = `date`;
        $params{conf_url} = $tuanzhang_url;
        $params{username} = $request->users->username;
        $params{comment} = $shenpi_comment;
        my $title = "营长认证申请被拒绝";
        my $template = "email/rejectTuanZhang.html.tt";
	my $tuanzhanguser = $c->model('Users::UsersSearchModel')->find($request->user_id);
        $sender->send($tuanzhanguser->email_address, $title, $template, \%params, \%options);
        $c->stash(
		login_user => $c->session->{login_user},
                css => 'tuanzhang.css',
                title => '营长认证申请',
                tuanzhang => $request,
                javascript => '/tuan/tuan_info.js',
                template => 'tuanzhang/tuanzhang_request_mainpage.tt2');
}

sub quxiao : Local {
	my ($self, $c) = @_;
	if (!$c->user_exists) {
                $c->response->redirect($c->uri_for('/login'));
                $c->flash->{redirect_after_login} = '' . $c->req->uri;
                return 0;
        }
	my $tuanzhang_id = $c->request->params->{tuanzhang_id};
	my $user_id = $c->session->{login_user}->id;
        my $request = $c->model('TuanZhang::TuanZhangModel')->find($tuanzhang_id);
	if ($request->status ne 'R') {
		my $message = "当前营长认证申请已经被管理员处理，无法撤销";
		$c->stash(login_user => $c->session->{login_user});
                $c->stash(css   => 'tuanzhang.css');
                $c->stash(message => $message);
                $c->stash(request => $request);
                $c->stash(template => 'tuanzhang/tuanzhang_info_failed.tt2');
		return 1;
	}
	if ($c->session->{login_user}->id ne $request->user_id) {
		my $message = "对不起，您没有权限进行当前操作，只有申请者本人才可以撤销申请";
		$c->stash(login_user => $c->session->{login_user});
                $c->stash(css   => 'tuanzhang.css');
                $c->stash(message => $message);
                $c->stash(request => $request);
                $c->stash(template => 'tuanzhang/tuanzhang_info_failed.tt2');
		return 1;
	}
        $request = $c->model('TuanZhang::TuanZhangModel')->update_status($tuanzhang_id,'C',$user_id);
        $c->stash(
                tuanzhang => $request,
                #javascript => '/topic/discuss.js',
		login_user => $c->session->{login_user},
		title => '营长认证申请',
		css => 'tuanzhang.css',
                template => 'tuanzhang/tuanzhang_request_mainpage.tt2');
}

sub xiugai : Local {
	my ($self, $c) = @_;
	if (!$c->user_exists) {
                $c->response->redirect($c->uri_for('/login'));
                $c->flash->{redirect_after_login} = '' . $c->req->uri;
                return 0;
        }
	
	my $tuanzhang_id = $c->request->params->{tuanzhang_id};
	my $user_id = $c->session->{login_user}->id;
        my $request = $c->model('TuanZhang::TuanZhangModel')->find($tuanzhang_id);
	if ($request->status ne 'C') {
		if ($request->status ne 'N') {
			my $message = "当前营长认证申请无法修改";
			$c->stash(login_user => $c->session->{login_user});
                	$c->stash(css   => 'tuanzhang.css');
                	$c->stash(message => $message);
                	$c->stash(request => $request);
                	$c->stash(template => 'tuanzhang/tuanzhang_info_failed.tt2');
			return 1;
		}
	}
	if ($c->session->{login_user}->id ne $request->user_id) {
		my $message = "对不起，您没有权限进行当前操作，只有申请者本人才可以修改申请";
		$c->stash(login_user => $c->session->{login_user});
                $c->stash(css   => 'tuanzhang.css');
                $c->stash(message => $message);
                $c->stash(request => $request);
                $c->stash(template => 'tuanzhang/tuanzhang_info_failed.tt2');
		return 1;
	}
	my $user = $c->model('Users::UsersSearchModel')->find($user_id);
	$c->stash(login_user => $c->session->{login_user},
                javascript => '/tuanzhang/edit_tuanzhang.js',
                tuanzhang => $request,
                css => 'create_new_tuan.css',
                user => $user,
                template => "tuanzhang/edit_tuanzhang.tt2");
}

sub tuanzhang_xiugai_do:Local {
        my ($self, $c) = @_;
	my $tz_id = $c->request->params->{tz_id};
        my $user_id = $c->request->params->{user_id};
        my $actual_name = $c->request->params->{actual_name};
        my $idno = $c->request->params->{idno};
        my $shouji = $c->request->params->{shouji};
        my $dizhi = $c->request->params->{dizhi};
        my $is_student = $c->request->params->{is_student};
        my $xuehao = $c->request->params->{xuehao};
        my $xibie = $c->request->params->{xibie};
        my $nianji = $c->request->params->{nianji};
        my $neirong = $c->request->params->{neirong};
        my $university_id = $c->request->params->{university_id};

        my $shenfengzheng = $c->request->upload('shenfengzheng');
        my $xueshengzheng = $c->request->upload('xueshengzheng');

        my $tuanzhang = $c->model('TuanZhang::TuanZhangModel')->update($tz_id ,$user_id,$actual_name,$idno,$shouji,$dizhi,$is_student,$xuehao,$xibie,$nianji,$shenfengzheng,$xueshengzheng,$neirong,$university_id,$c->config->{image_path});

        $c->response->redirect($c->uri_for($c->controller('tuanzhang')->action_for('tuanzhang_info'), [$tuanzhang->id]));
}

sub tuanzhangobject :Chained('base') :PathPart('id') :CaptureArgs(1) {
        my ($self, $c, $id) = @_;
        $c->stash(object => $c->model('TuanZhang::TuanZhangModel')->find($id));
        $c->stash(id => $id);
}

sub tuanzhang_info :Chained('tuanzhangobject') :PathPart('tuanzhang_info') :Args(0) {
        my ($self, $c) = @_;
	if (!$c->user_exists) {
                $c->response->redirect($c->uri_for('/login'));
                return 0;
        }
        my $tuanzhang = $c->model('TuanZhang::TuanZhangModel')->find($c->stash->{object}->id);
        my $current_id = $c->session->{login_user}->id;
        my $is_admin = $c->model('UserTuan::UserTuanModel')->is_admin($c->session->{login_user},$c->session->{login_user});
        if($current_id.'' ne $tuanzhang->user_id.'' && $is_admin ne '1') {
                my $message = "只有申请者本人和管理员可以看到本认证信息";
                $c->stash(login_user => $c->session->{login_user});
                $c->stash(css   => 'tuanzhang.css');
                $c->stash(message => $message);
                $c->stash(request => $tuanzhang);
                $c->stash(template => 'tuanzhang/tuanzhang_info_failed.tt2');
                return 1;
        }
        $c->stash(
                login_user => $c->session->{login_user},
                css => 'tuanzhang.css',
                title => '营长认证申请',
                tuanzhang => $tuanzhang,
                javascript => '/tuan/tuan_info.js',
                template => 'tuanzhang/tuanzhang_request_mainpage.tt2');
}

sub tuanzhang_request_do:Local {
        my ($self, $c) = @_;
	if (!$c->user_exists) {
                $c->response->redirect($c->uri_for('/login'));
                return 0;
        }
        my $user_id = $c->request->params->{user_id};
        my $actual_name = $c->request->params->{actual_name};
        my $idno = $c->request->params->{idno};
        my $shouji = $c->request->params->{shouji};
        my $dizhi = $c->request->params->{dizhi};
        my $is_student = $c->request->params->{is_student};
        my $xuehao = $c->request->params->{xuehao};
        my $xibie = $c->request->params->{xibie};
        my $nianji = $c->request->params->{nianji};
        my $neirong = $c->request->params->{neirong};
        my $university_id = $c->request->params->{university_id};

        my $shenfengzheng = $c->request->upload('shenfengzheng');
        my $xueshengzheng = $c->request->upload('xueshengzheng');

        my $tuanzhang = $c->model('TuanZhang::TuanZhangModel')->create($user_id,$actual_name,$idno,$shouji,$dizhi,$is_student,$xuehao,$xibie,$nianji,$shenfengzheng,$xueshengzheng,$neirong,$university_id,$c->config->{image_path});

        $c->response->redirect($c->uri_for($c->controller('tuanzhang')->action_for('tuanzhang_info'), [$tuanzhang->id]));
}

sub tuanzhang_request:Local {
        my ($self, $c) = @_;
	if (!$c->user_exists) {
                $c->response->redirect($c->uri_for('/login'));
                return 0;
        }
        my $user_id = $c->request->params->{user_id};

        my $user = $c->model('Users::UsersSearchModel')->find($user_id);
        $c->stash(login_user => $c->session->{login_user},
                javascript => '/tuanzhang/request_tuanzhang.js',
                css => 'create_new_tuan.css',
                user => $user,
                template => "tuanzhang/request_tuanzhang.tt2");
}


=head1 AUTHOR

lijie,,,

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

__PACKAGE__->meta->make_immutable;

1;
